package com.controller;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Map;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;

import com.entity.JiaoshiyundongxiangmuEntity;
import com.entity.JiaoshibaomingxinxiEntity;
import com.entity.JiaoshiyundongxiangmuEntity;
import com.service.JiaoshiyundongxiangmuService;
import com.utils.ValidatorUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.annotation.IgnoreAuth;

import com.entity.JiaoshibaomingxinxiEntity;
import com.entity.view.JiaoshibaomingxinxiView;

import com.service.JiaoshibaomingxinxiService;
import com.service.TokenService;
import com.utils.PageUtils;
import com.utils.R;
import com.utils.MPUtil;
import com.utils.MapUtils;
import com.utils.CommonUtil;
import java.io.IOException;

/**
 * 教师报名信息
 * 后端接口
 * @author 
 * @email 
 * @date 2024-03-01 11:30:28
 */
@RestController
@RequestMapping("/jiaoshibaomingxinxi")
public class JiaoshibaomingxinxiController {
    @Autowired
    private JiaoshibaomingxinxiService jiaoshibaomingxinxiService;

    @Autowired
    private JiaoshiyundongxiangmuService jiaoshiyundongxiangmuService;



    



    /**
     * 后端列表
     */
    @RequestMapping("/page")
    public R page(@RequestParam Map<String, Object> params,JiaoshibaomingxinxiEntity jiaoshibaomingxinxi,
		HttpServletRequest request){
		String tableName = request.getSession().getAttribute("tableName").toString();
		if(tableName.equals("jiaoshi")) {
			jiaoshibaomingxinxi.setJiaoshigonghao((String)request.getSession().getAttribute("username"));
		}
        EntityWrapper<JiaoshibaomingxinxiEntity> ew = new EntityWrapper<JiaoshibaomingxinxiEntity>();

		PageUtils page = jiaoshibaomingxinxiService.queryPage(params, MPUtil.sort(MPUtil.between(MPUtil.likeOrEq(ew, jiaoshibaomingxinxi), params), params));

        return R.ok().put("data", page);
    }
    
    /**
     * 前端列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params,JiaoshibaomingxinxiEntity jiaoshibaomingxinxi, 
		HttpServletRequest request){
        EntityWrapper<JiaoshibaomingxinxiEntity> ew = new EntityWrapper<JiaoshibaomingxinxiEntity>();

		PageUtils page = jiaoshibaomingxinxiService.queryPage(params, MPUtil.sort(MPUtil.between(MPUtil.likeOrEq(ew, jiaoshibaomingxinxi), params), params));
        return R.ok().put("data", page);
    }



	/**
     * 列表
     */
    @RequestMapping("/lists")
    public R list( JiaoshibaomingxinxiEntity jiaoshibaomingxinxi){
       	EntityWrapper<JiaoshibaomingxinxiEntity> ew = new EntityWrapper<JiaoshibaomingxinxiEntity>();
      	ew.allEq(MPUtil.allEQMapPre( jiaoshibaomingxinxi, "jiaoshibaomingxinxi")); 
        return R.ok().put("data", jiaoshibaomingxinxiService.selectListView(ew));
    }

	 /**
     * 查询
     */
    @RequestMapping("/query")
    public R query(JiaoshibaomingxinxiEntity jiaoshibaomingxinxi){
        EntityWrapper< JiaoshibaomingxinxiEntity> ew = new EntityWrapper< JiaoshibaomingxinxiEntity>();
 		ew.allEq(MPUtil.allEQMapPre( jiaoshibaomingxinxi, "jiaoshibaomingxinxi")); 
		JiaoshibaomingxinxiView jiaoshibaomingxinxiView =  jiaoshibaomingxinxiService.selectView(ew);
		return R.ok("查询教师报名信息成功").put("data", jiaoshibaomingxinxiView);
    }
	
    /**
     * 后端详情
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
        JiaoshibaomingxinxiEntity jiaoshibaomingxinxi = jiaoshibaomingxinxiService.selectById(id);
        return R.ok().put("data", jiaoshibaomingxinxi);
    }

    /**
     * 前端详情
     */
    @RequestMapping("/detail/{id}")
    public R detail(@PathVariable("id") Long id){
        JiaoshibaomingxinxiEntity jiaoshibaomingxinxi = jiaoshibaomingxinxiService.selectById(id);
        return R.ok().put("data", jiaoshibaomingxinxi);
    }
    



    /**
     * 后端保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody JiaoshibaomingxinxiEntity jiaoshibaomingxinxi, HttpServletRequest request){
    	//ValidatorUtils.validateEntity(jiaoshibaomingxinxi);
        jiaoshibaomingxinxiService.insert(jiaoshibaomingxinxi);
        return R.ok();
    }
    
    /**
     * 前端保存
     */
    @RequestMapping("/add")
    public R add(@RequestBody JiaoshibaomingxinxiEntity jiaoshibaomingxinxi, HttpServletRequest request){
    	//ValidatorUtils.validateEntity(jiaoshibaomingxinxi);
        jiaoshibaomingxinxiService.insert(jiaoshibaomingxinxi);
        EntityWrapper<JiaoshiyundongxiangmuEntity> ew = new EntityWrapper<>();
        ew.eq("yundongmingcheng",jiaoshibaomingxinxi.getYundongmingcheng());
        JiaoshiyundongxiangmuEntity jiaoshiyundongxiangmuEntity=  jiaoshiyundongxiangmuService.selectView(ew);
        JiaoshiyundongxiangmuEntity jiaoshibaomingxinxiEntity = new JiaoshiyundongxiangmuEntity();
        jiaoshibaomingxinxiEntity.setCansairenshu(String.valueOf(Integer.valueOf(jiaoshiyundongxiangmuEntity.getCansairenshu()) + 1));
        jiaoshiyundongxiangmuService.update(jiaoshibaomingxinxiEntity, ew);
        return R.ok();
    }





    /**
     * 修改
     */
    @RequestMapping("/update")
    @Transactional
    public R update(@RequestBody JiaoshibaomingxinxiEntity jiaoshibaomingxinxi, HttpServletRequest request){
        //ValidatorUtils.validateEntity(jiaoshibaomingxinxi);
        jiaoshibaomingxinxiService.updateById(jiaoshibaomingxinxi);//全部更新
        return R.ok();
    }



    

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
        jiaoshibaomingxinxiService.deleteBatchIds(Arrays.asList(ids));
        return R.ok();
    }




    @RequestMapping("/queryCount/{id}/{userId}")
    public R queryCount(@PathVariable("id") Long id,@PathVariable("userId") Long userId ){
        EntityWrapper<JiaoshibaomingxinxiEntity> ew = new EntityWrapper< JiaoshibaomingxinxiEntity>();
        ew.eq("crossuserid",userId);
        ew.eq("crossrefid",id);
        int i = jiaoshibaomingxinxiService.selectCount(ew);
        return R.ok().put("data", (i > 0)?true:false);
    }

    @RequestMapping("/deleteBaoming/{id}/{userId}")
    public R deleteBaoming(@PathVariable("id") Long id,@PathVariable("userId") Long userId ){
        EntityWrapper<JiaoshiyundongxiangmuEntity> ew = new EntityWrapper<>();
        JiaoshiyundongxiangmuEntity jiaoshiyundongxiangmuEntity =  jiaoshiyundongxiangmuService.selectById(id);
        JiaoshiyundongxiangmuEntity jiaoshibaomingxinxiEntity = new JiaoshiyundongxiangmuEntity();
        jiaoshibaomingxinxiEntity.setCansairenshu(String.valueOf(Integer.valueOf(jiaoshiyundongxiangmuEntity.getCansairenshu()) - 1));
        jiaoshiyundongxiangmuService.update(jiaoshibaomingxinxiEntity, ew);

        EntityWrapper<JiaoshibaomingxinxiEntity> ew1 = new EntityWrapper< JiaoshibaomingxinxiEntity>();
        ew1.eq("crossuserid",userId);
        ew1.eq("crossrefid",id);
        boolean delete = jiaoshibaomingxinxiService.delete(ew1);
        return R.ok().put("code", (delete)?0:-1);
    }








}
